package cn.chinasyq.photoquan.photo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.List;

import cn.chinasyq.photoquan.R;
import cn.chinasyq.photoquan.photo.adapter.PhotoPagerTabAdapter;
import cn.chinasyq.photoquan.photo.bean.PhotoInfo;
import cn.chinasyq.photoquan.photo.bean.PhotoesEntity;
import cn.chinasyq.photoquan.photo.fragment.PhotoInfoFragment;
import cn.chinasyq.photoquan.photo.mode.PhotoMode;
import cn.chinasyq.photoquan.user.activity.LoginActivity;
import cn.chinasyq.photoquan.user.mode.UserMode;
import cn.chinasyq.photoquan.util.ShareSDKUtil;
import cn.master.util.utils.ToastUtil;
import cn.master.volley.models.response.handler.ResponseHandler;

public class PhotoInfoActivity extends FragmentActivity implements View.OnClickListener, ResponseHandler.OnSucceedListener, ResponseHandler.OnFailedListener, ResponseHandler.OnNeedLoginListener {
    private static final String TAG_REFRESH = "refresh";
    public static final String UUID = "uuid";
    private static final String TAG_UP = "up";
    private ViewPager pager;
    private PhotoPagerTabAdapter adapter;
    private List<PhotoesEntity> photos;
    private TextView tv_title;
    private String uuid;
    private Button btn_share;
    private Button btn_comment;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo_info);
        pager = (ViewPager) findViewById(R.id.gallery);
        adapter = new PhotoPagerTabAdapter(this, pager);
        btn_share = (Button) findViewById(R.id.btn_share);
        btn_share.setOnClickListener(this);
        uuid = getIntent().getStringExtra(UUID);
        pager.setOffscreenPageLimit(0);
        tv_title = (TextView) findViewById(R.id.tv_title);
        btn_comment = (Button)findViewById(R.id.btn_comment);
        tv_title.setText("0/0");
        pager.setOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageSelected(int arg0) {
                tv_title.setText((arg0 + 1) + "/" + photos.size());
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
    }
    @Override
    protected void onResume() {
        super.onResume();
        refresh();
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.image_right:
                finish();
                break;
            case R.id.btn_share:
                initShareData(null);
                break;
            case R.id.btn_comment:
                upComment(uuid);
                break;
            default:
                break;
        }

    }

    protected void upComment(String uuid) {
        Intent intent = new Intent(this, PhotoCommentActivity.class);
        intent.putExtra(PhotoCommentActivity.UUID, uuid);
        intent.putExtra(PhotoCommentActivity.TYPE,"Exhibition");
        startActivity(intent);
    }

    private void initShareData(String name) {
        if (info == null || info.getShare() == null) {
            return;
        }
        ShareSDKUtil.showShare(this, name, info.getShare().getTitle(),
                info.getShare().getSummary(),
                info.getShare().getImage(),
                info.getShare().getUrl());
    }

    @Override
    protected void onDestroy() {
        // YTShareUtil.release(this);
        super.onDestroy();
    }

    void refresh() {
        ResponseHandler handler = new ResponseHandler(TAG_REFRESH);
        handler.setOnSucceedListener(this);
        handler.setOnFailedListener(this);
        PhotoMode.obtainPhotoInfo(UserMode.getCurrentUserInfo(this).getToken(), uuid, handler);
    }

    @Override
    public void onFailed(String tag, ResponseHandler.FailedStatus status, int resultCode, String msg) {
        if (msg == null) {
            switch (status) {
                case DATA_ERROR:
                    ToastUtil.show(getApplicationContext(), R.string.tip_data_error);
                    break;
                case NETWORK_ERROR:
                    ToastUtil.show(getApplicationContext(), R.string.servererrortips);
                    break;
            }
        } else {
            ToastUtil.show(getApplicationContext(), msg);
        }
    }

    @Override
    public void onNeedLogin(String tag) {
        startActivity(new Intent(this, LoginActivity.class));
    }

    private PhotoInfo info;

    @Override
    public void onSucceed(String tag, boolean isCache, Object data) {
        if (tag.equals(TAG_REFRESH)) {
            info = (PhotoInfo) data;
            photos = info.getPhotos();
            for (int i = 0; i < photos.size(); i++) {
                Bundle bundle = new Bundle();
                bundle.putString("url", photos.get(i).getImage());
                bundle.putString("desc", photos.get(i).getDesc());
                adapter.addItemInfo(0x100 + i, "",
                        PhotoInfoFragment.class, bundle);
            }
            btn_comment.setText(info.getComments_count()+"跟帖");
            tv_title.setText(1 + "/" + photos.size());
            if (info.getShare() == null) {
                btn_share.setVisibility(View.GONE);
            } else {
                btn_share.setVisibility(View.VISIBLE);
            }
        }
    }
}
